package leetcode.code0918;

public class Solution extends Solution918 {

	@Override
	public int maxSubarraySumCircular(int[] nums) {
		int Len = nums.length;
		int max0 = nums[0];// 连续最大
		int max1 = nums[0];// 之前最大
		int min0 = nums[0];// 连续最小
		int min1 = nums[0];// 之前最小
		int sum = nums[0];
		for (int i = 1; i < Len; i++) {
			int curNum = nums[i];
			int curMax0 = Math.max(curNum, max0 + curNum);// 连续最大
			int curMax1 = Math.max(max1, curMax0);
			max0 = curMax0;
			max1 = curMax1;
			int curMin0 = Math.min(curNum, min0 + curNum);// 连续最小
			int curMin1 = Math.min(min1, curMin0);
			min0 = curMin0;
			min1 = curMin1;
			sum += curNum;
		}
		return sum == min1 ? max1 : Math.max(max1, sum - min1);
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		so.debug5();
	}

}
